/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 14, 2012  3:52:43 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : ProjectServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class ProjectServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(ProjectServices.class);
	
	public ProjectServices(EntityManager em) {
		super(em);
	}
	
	public List<CostProject> findAll() throws Exception {
		
		return super.ConvertToYoureObject(super.findAll("CostProject.findAll"));				
	}
	
	public CostProject findById(CostProjectPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "CostProject.findById"));				
	}
		
	public CostProject findByPlantCode(String sap_plant_code) throws Exception {
		try{
			
			Map<String,Object> mapWhereValue = new HashMap<String, Object>();
			mapWhereValue.put("sapPlantCode", sap_plant_code);			
			
			List<CostProject> lst = super.ConvertToYoureObject(super.find("CostProject.findByPlant", mapWhereValue));
			if(!CommonService.IsNull(lst))
			{
				if(CommonService.getSize(lst) == 1)
					return lst.get(0);
				else
					throw new Exception("Exception : More Record(s) Size="+ CommonService.getSize(lst) +" ");
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		return null;
	}
	
	public List<CostProject> findByStatus(String status) throws Exception {
		try{
			Map<String,Object> mapWhereValue = new HashMap<String, Object>();
			mapWhereValue.put("status", status);
			
			List<CostProject> lst = super.ConvertToYoureObject(super.find("CostProject.findByStatus", mapWhereValue));
			if(CommonService.getSize(lst) > 0)
				return lst;
			else
				return null;
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}		
	}	
	
	
	public CostProject InsertedOrUpdated(CostProject vlaue) throws Exception {
		CostProject obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{			
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				
				super.inserted(obj);
				
			}else{			
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public CostProject Inserted(CostProject vlaue) throws Exception {
		CostProject obj = vlaue;
		try{	
			obj.setSyncCreatedby(this.getUser());
			obj.setSyncCreatedon(this.getCurrent_date());
			obj.setSyncUpdatedby(this.getUser());
			obj.setSyncUpdatedon(this.getCurrent_date());
			
			super.inserted(obj);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	
	public CostProject Updated(CostProject vlaue) throws Exception {
		CostProject obj = vlaue;
		try{	
			obj.setSyncUpdatedby(this.getUser());
			obj.setSyncUpdatedon(this.getCurrent_date());
			super.updated(obj);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<CostProject> Inserted(List<CostProject> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(CostProject obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{		
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<CostProject> Updated(List<CostProject> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(CostProject obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				
				common_lst.add(obj);
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	

}
